package app

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/class"
	"github.com/lab-online/internal/course"
	"github.com/lab-online/internal/lab"
	"github.com/lab-online/internal/report"
	"github.com/lab-online/internal/user"
)

type Route interface {
	Register(router *gin.RouterGroup)
}

func (app *Context) RegisterRoutes(prefix string) {
	routes := app.BuildRoutes()

	base := app.engine.Group(prefix)
	v1 := base.Group("/v1")

	for _, route := range routes {
		route.Register(v1)
	}
}

func (app *Context) BuildRoutes() []Route {
	classRepository := class.NewRepository(app.DB)
	courseRepository := course.NewRepository(app.DB)
	reportRepository := report.NewRepository(app.DB)
	userRepository := user.NewRepository(app.DB)

	labMQ := lab.NewMQ(app.MessageQueue)
	reportMQ := report.NewMQ(app.MessageQueue)

	labOSS := lab.NewOSS(app.OSS)

	courseExport := course.NewExport(courseRepository)
	classExport := class.NewExport(classRepository)
	userExport := user.NewExport(userRepository)
	reportExport := report.NewExport(reportRepository)

	classDomain := class.NewDomain(classRepository, courseExport, userExport)
	courseDomain := course.NewDomain(courseRepository)
	reportDomain, cancel := report.NewDomain(reportRepository, reportMQ)
	app.effects = append(app.effects, cancel)

	userDomain := user.NewDomain(userRepository, app.jwt)
	labDomain := lab.NewDomain(labMQ, labOSS, reportExport, classExport, courseExport)

	userRoute := user.NewRoute(app.auth, userDomain)
	courseRoute := course.NewRoute(app.auth, courseDomain)
	reportRoute := report.NewRoute(app.auth, reportDomain)
	classRoute := class.NewRoute(app.auth, classDomain)
	labRoute := lab.NewRoute(app.auth, labDomain)

	return []Route{
		labRoute,
		userRoute,
		classRoute,
		reportRoute,
		courseRoute,
	}
}
